package com.jkoss.wine.merchantOrder.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jkoss.base.controller.BaseController;
import com.jkoss.common.util.CommonUtil;
import com.jkoss.common.vo.DwzPageBean;
import com.jkoss.wine.config.mvc.MoneyExcelHandler;
import com.jkoss.wine.merchant.service.IMerchantProductNormService;
import com.jkoss.wine.merchantOrder.entity.MerchantOrder;
import com.jkoss.wine.merchantOrder.service.IHqProductInventoryService;
import com.jkoss.wine.merchantOrder.service.IMerchantOrderService;
import com.jkoss.wine.merchantOrder.vo.AccountTypeExcelVo;
import com.jkoss.wine.merchantOrder.vo.ListOrderExcelVo;
import com.jkoss.wine.merchantOrder.vo.MerchantOrderVo;
import com.jkoss.wine.merchantOrder.vo.OrderFlowExcelVo;
import com.jkoss.wine.merchantOrder.vo.ShopListExcelVo;
import com.jkoss.wine.shop.entity.Shop;
import com.jkoss.wine.shop.service.IShopService;
import com.jkoss.wine.system.entity.Areas;
import com.jkoss.wine.system.entity.Department;
import com.jkoss.wine.system.entity.MerchantEmployees;
import com.jkoss.wine.system.entity.Role;
import com.jkoss.wine.system.service.IAreasService;
import com.jkoss.wine.system.service.IDepartmentService;
import com.jkoss.wine.system.service.IMerchantEmployeesRoleService;
import com.jkoss.wine.system.service.IMerchantEmployeesService;
import com.jkoss.wine.system.service.IRoleService;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.handler.inter.IExcelDataHandler;

/**
 * 前端控制器
 * 
 * @Author Ning
 * @Version 1.0, 2018-10-05
 * @See
 * @Since com.jkoss.wine.merchantOrder.controller
 * @Description: TODO
 */
@Controller
@RequestMapping("/merchantOrder/merchantOrder")
public class MerchantOrderController extends BaseController {

	@Autowired
	private IMerchantOrderService iMerchantOrderService;
	@Autowired
	private IMerchantEmployeesService iMerchantEmployeesService;
	@Autowired
	private IDepartmentService iDepartmentService;
	@Autowired
	private IHqProductInventoryService iHqProductInventoryService;
	@Autowired
	private IAreasService iAreasService;
	@Autowired
	private IMerchantEmployeesRoleService iMerchantEmployeesRoleService;
	@Autowired
	private IRoleService iRoleService;
	// @Autowired
	// private IWareInoutOrderDetailsService iWareInoutOrderDetailsService;
	@Autowired
	private IShopService iShopService;

	@Autowired
	private IMerchantProductNormService iMerchantProductNormService;

	/**
	 * 列表
	 * 
	 * @param areaCode
	 * @param orderProcess
	 * @param startTime
	 * @param endTime
	 * @param orderNum
	 * @param shopName
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/list")
	@RequiresPermissions("/merchantOrder/merchantOrder/list")
	public String list(String areaCode, String orderProcess, String startTime, String endTime, String orderNum, String shopName, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception {
		String ordernum = request.getParameter("org2.orderNum");
		String shopname = request.getParameter("org2.shopName");
		// 省市级
		Wrapper wrapper1 = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper1);
		map.put("provinces", provinces);
		String repAreaCode = "";
		if (!CommonUtil.isBlank(areaCode)) {
			String[] areas = areaCode.split(",");
			for (int i = 0; i < areas.length; i++) {
				repAreaCode += areas[i];
				if (i != 2 && areas.length - 1 > i && !CommonUtil.isBlank(areas[i + 1])) {
					repAreaCode += ",";
				}
			}
			if (areas.length >= 1 && !CommonUtil.isBlank(areas[0])) {
				dwzPageBean.getCountResultMap().put("province", areas[0]);
				if (areas.length >= 2 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("city", areas[1]);
				}
				// 市
				String city = areas[0].substring(0, 2);
				Wrapper cityWrapper = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
				List<Areas> citys = iAreasService.selectList(cityWrapper);
				map.put("citys", citys);
			}
			if (areas.length >= 2 && !CommonUtil.isBlank(areas[1])) {
				if (areas.length >= 3 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("county", areas[2]);
				}
				// 区
				String county = areas[1].substring(0, 4);
				Wrapper countyWrapper = Condition.create().andNew("id like '" + county + "%'").ne("id", county + "00");
				List<Areas> countys = iAreasService.selectList(countyWrapper);
				map.put("countys", countys);
			}
		}
		// 根据用户id获取角色
		Wrapper wrapper = Condition.create().ne("mo.del", 1).like("order_process", orderProcess).like("order_num", ordernum).like("shop_name", shopname).like("s.area_code", repAreaCode);
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		// 获取当前用户id
		MerchantEmployees merchantEmployees = iMerchantEmployeesService.selectById(getUserId());
		// 根据当前用户id获取用户所在部门
		if (!CommonUtil.isBlank(merchantEmployees.getDmid())) {
			Department department = iDepartmentService.selectById(merchantEmployees.getDmid());
			if (!CommonUtil.isBlank(department)) {
				map.put("depart", department.getName());
			}
		}
		dwzPageBean.getCountResultMap().put("areaCode", areaCode);
		dwzPageBean.getCountResultMap().put("orderNum", ordernum);
		dwzPageBean.getCountResultMap().put("shopName", shopname);
		dwzPageBean.getCountResultMap().put("startTime", startTime);
		dwzPageBean.getCountResultMap().put("endTime", endTime);
		if (!CommonUtil.isBlank(orderProcess)) {
			dwzPageBean.getCountResultMap().put("orderProcess", orderProcess);
		}
		if (!CommonUtil.isBlank(startTime)) {
			wrapper.ge("mo.add_time", startTime);
		}
		if (!CommonUtil.isBlank(endTime)) {
			wrapper.le("mo.add_time", CommonUtil.dateLastTime(endTime));
		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("mo.add_time", false);
		}

		Page resultPage = iMerchantOrderService.selectOrderPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantOrder/merchantOrder/list";
	}

	/**
	 * 订单详情
	 * 
	 * @param orid
	 * @param map
	 * @return
	 */
	@RequestMapping("/OrderList")
	@RequiresPermissions("/merchantOrder/merchantOrder/OrderList")
	public String MerchantOrderInfolist(String orid, ModelMap map) {
		map.put("orderinfo2", iMerchantOrderService.selectById(orid));
		Wrapper wrapper = Condition.create().eq("mb.merchant_order_id", orid);
		map.put("orderInfo", iMerchantOrderService.selectVoByorid(wrapper));
		return "merchantOrder/merchantOrder/OrderList";
	}

	/**
	 * 去审核
	 * 
	 * @param orid
	 * @param map
	 * @return
	 */
	@RequestMapping("/toReview")
	@RequiresPermissions("/merchantOrder/merchantOrder/updateOrder")
	public String toReview(String id, ModelMap map) {
		map.put("id", id);
		return "merchantOrder/merchantOrder/review";
	}

	/**
	 * 更新订单状态
	 * 
	 * @param merchant_order
	 * @param id
	 * @param request
	 * @param response
	 * @param str
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/updateOrder")
	@RequiresPermissions("/merchantOrder/merchantOrder/updateOrder")
	@ResponseBody
	public Object updateOrder(@Valid MerchantOrder merchant_order, String id, HttpServletRequest request, HttpServletResponse response, String str) throws Exception {
		merchant_order = iMerchantOrderService.selectById(id);
		if (merchant_order == null) {
			return ajaxError("找不到该条记录");
		}
		if (merchant_order.getOrderProcess() == 9) {
			return ajaxError("订单已完成，不可执行此操作");
		}
		merchant_order.setUpdateTime(CommonUtil.getDate());
		// 受理
		if ("accept".equals(str)) {
			if (merchant_order.getOrderProcess() == 0) {
				merchant_order.setOrderProcess(1);
				String rvrm = request.getParameter("rvrm");
				merchant_order.setRvrm(rvrm);
				merchant_order.setRvid(getUserId());
				if (iMerchantOrderService.updateById(merchant_order)) {
					return ajaxSuccess();
				}
			}
			return ajaxError("不是新订单，不允许执行该操作");
		}
		// 发货
		if ("send".equals(str)) {
			if (merchant_order.getOrderProcess() == 1) {

				// Wrapper wrapper = Condition.create().eq("ioid",
				// merchant_order.getMerchantOrderId());
				// List<WareInoutOrderDetails> details =
				// iWareInoutOrderDetailsService.selectList(wrapper);
				//
				//// Wrapper wrapper = Condition.create().eq("mo.merchant_order_id", id);
				// // 库存数量、购物车数量
				// List<MerchantOrderVo> minventory =
				// iMerchantOrderService.selectVoByoridNormid(wrapper);
				// for (MerchantOrderVo m : minventory) {
				// if (m.getBcount() > m.getHcount()) {
				// return ajaxError("库存不足，请备货");
				// }
				// }
				// merchant_order.setOrderProcess(2);
				// if (iMerchantOrderService.updateById(merchant_order)) {
				// return ajaxSuccess();
				// }
				if (iMerchantOrderService.updateShip(merchant_order)) {
					return ajaxSuccess();
				}
			}
			return ajaxError();
		}
		// 取消订单
		if ("cancel".equals(str)) {
			if (merchant_order.getOrderProcess() != 8) {
				merchant_order.setOrderProcess(8);
				if (iMerchantOrderService.updateById(merchant_order)) {
					return ajaxSuccess();
				}
			}
			return ajaxError();
		}
		return ajaxSuccess();
	}

	/**
	 * 完成订单
	 * 
	 * @param merchant_order
	 * @param id
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/finishOrder")
	@RequiresPermissions("/merchantOrder/merchantOrder/finishOrder")
	@ResponseBody
	public Object finishOrder(@Valid MerchantOrder merchant_order, String id, HttpServletRequest request, HttpServletResponse response) throws Exception {
		merchant_order = iMerchantOrderService.selectById(id);
		merchant_order.setUpdateTime(CommonUtil.getDate());
		if (merchant_order.getOrderProcess() == 9) {
			return ajaxError();
		}
		if (merchant_order.getOrderProcess() == 8) {
			return ajaxError();
		}
		if (merchant_order.getOrderProcess() == 2) {
			merchant_order.setOrderProcess(9);
			if (iMerchantOrderService.insertMerchantBasket(merchant_order)) {
				return ajaxSuccess();
			}
		}
		return ajaxError();
	}

	/**
	 * 门店列表
	 * 
	 * @param merchant_order
	 * @param shopName
	 * @param areaCode
	 * @param state
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/financial/financiallist")
	@RequiresPermissions("/merchantOrder/merchantOrder/financial/financiallist")
	public Object financiallist(@Valid MerchantOrder merchant_order, String shopName, String areaCode, String state, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		// 门店查询
		String shopname = request.getParameter("org2.shopName");
		// 省市级
		Wrapper wrapper1 = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper1);
		map.put("provinces", provinces);
		String repAreaCode = "";
		if (!CommonUtil.isBlank(areaCode)) {
			String[] areas = areaCode.split(",");
			for (int i = 0; i < areas.length; i++) {
				repAreaCode += areas[i];
				if (i != 2 && areas.length - 1 > i && !CommonUtil.isBlank(areas[i + 1])) {
					repAreaCode += ",";
				}
			}
			if (areas.length >= 1 && !CommonUtil.isBlank(areas[0])) {
				dwzPageBean.getCountResultMap().put("province", areas[0]);
				if (areas.length >= 2 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("city", areas[1]);
				}
				// 市
				String city = areas[0].substring(0, 2);
				Wrapper cityWrapper = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
				List<Areas> citys = iAreasService.selectList(cityWrapper);
				map.put("citys", citys);
			}
			if (areas.length >= 2 && !CommonUtil.isBlank(areas[1])) {
				if (areas.length >= 3 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("county", areas[2]);
				}
				// 区
				String county = areas[1].substring(0, 4);
				Wrapper countyWrapper = Condition.create().andNew("id like '" + county + "%'").ne("id", county + "00");
				List<Areas> countys = iAreasService.selectList(countyWrapper);
				map.put("countys", countys);
			}
		}
		Wrapper wrapper = Condition.create().ne("shop_state", 0).ne("del", 1).like("shop_name", shopname).like("s.area_code", repAreaCode);
		dwzPageBean.getCountResultMap().put("shopName", shopname);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("ocount", false);
			wrapper.orderBy("omoney", false);
		}
		Page resultPage = iMerchantOrderService.selectVoByshopid(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantOrder/merchantOrder/financial/list";
	}

	/**
	 * 订单详情
	 * 
	 * @param merchant_order
	 * @param shopId
	 * @param addTime
	 * @param areaCode
	 * @param orderNum
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/financial/orderlist")
	@RequiresPermissions("/merchantOrder/merchantOrder/financial/orderlist")
	public Object orderlist(@Valid MerchantOrder merchant_order, String shopId, String addTime, String areaCode, String orderNum, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		map.put("shopId", shopId);
		String ordernum = request.getParameter("org2.orderNum");
		// 规定时间的格式，只取月份
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM");
		// 获取当前时间
		Date current = CommonUtil.getDate();
		// 格式化当前日期用于第一次回显
		SimpleDateFormat sdf3 = new SimpleDateFormat("yyyy-MM-dd");
		String currentFormat = sdf3.format(current);
		dwzPageBean.getCountResultMap().put("addTime", currentFormat);
		// 格式化当前日期成月份
		String dfmStr = sdf.format(current);
		if (!CommonUtil.isBlank(addTime)) {
			Date date0 = sdf2.parse(addTime);
			dfmStr = sdf.format(date0);
			dwzPageBean.getCountResultMap().put("addTime", addTime);
		}
		// 省市级
		Wrapper wrapper1 = Condition.create().like("id", "0000");
		List<Areas> provinces = iAreasService.selectList(wrapper1);
		map.put("provinces", provinces);
		dwzPageBean.getCountResultMap().put("orderNum", ordernum);
		String repAreaCode = "";
		if (!CommonUtil.isBlank(areaCode)) {
			String[] areas = areaCode.split(",");
			for (int i = 0; i < areas.length; i++) {
				repAreaCode += areas[i];
				if (i != 2 && areas.length - 1 > i && !CommonUtil.isBlank(areas[i + 1])) {
					repAreaCode += ",";
				}
			}
			if (areas.length >= 1 && !CommonUtil.isBlank(areas[0])) {
				dwzPageBean.getCountResultMap().put("province", areas[0]);
				if (areas.length >= 2 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("city", areas[1]);
				}
				// 市
				String city = areas[0].substring(0, 2);
				Wrapper cityWrapper = Condition.create().andNew("id like '" + city + "%00'").ne("id", city + "0000");
				List<Areas> citys = iAreasService.selectList(cityWrapper);
				map.put("citys", citys);
			}
			if (areas.length >= 2 && !CommonUtil.isBlank(areas[1])) {
				if (areas.length >= 3 && !CommonUtil.isBlank(areas[0])) {
					dwzPageBean.getCountResultMap().put("county", areas[2]);
				}
				// 区
				String county = areas[1].substring(0, 4);
				Wrapper countyWrapper = Condition.create().andNew("id like '" + county + "%'").ne("id", county + "00");
				List<Areas> countys = iAreasService.selectList(countyWrapper);
				map.put("countys", countys);
			}
		}
		Wrapper wrapper2 = Condition.create().eq("mo.shop_id", shopId).like("order_num", ordernum).like("mo.add_time", dfmStr).like("s.area_code", repAreaCode);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper2.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper2.orderBy("mo.add_time", false);
		}
		Page resultPage = iMerchantOrderService.selectShopOL(dwzPageBean.toPage(), wrapper2);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantOrder/merchantOrder/financial/orderlist";
	}

	/**
	 * 订单分账列表
	 * 
	 * @param merchant_order
	 * @param state
	 * @param accountType
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/accountType/list")
	@RequiresPermissions("/merchantOrder/merchantOrder/accountType/list")
	public Object list(@Valid MerchantOrder merchant_order, String state, String accountType, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		List<Role> rolelist = getRoleList();
		map.put("rolelist", rolelist);
		Wrapper wrapper = Condition.create().eq("mo.order_process", 9).orderBy("mo.complete_time", false).like("account_type", accountType);
		if (!CommonUtil.isBlank(accountType)) {
			dwzPageBean.getCountResultMap().put("accountType", accountType);
		}
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("mo.add_time", false);
		}
		Page resultPage = iMerchantOrderService.selectVoAccountType(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantOrder/merchantOrder/accountType/list";
	}

	/**
	 * 订单分账逻辑
	 * 
	 * @param merchant_order
	 * @param id
	 * @param atype
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/accountType/updateAccount")
	@RequiresPermissions("/merchantOrder/merchantOrder/accountType/updateAccount")
	@ResponseBody
	public Object updateAccount(@Valid MerchantOrder merchant_order, String id, int atype, ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		merchant_order = iMerchantOrderService.selectById(id);
		if (atype == 0) {
			if (!CommonUtil.isBlank(merchant_order.getAccountType())) {
				if (merchant_order.getAccountType().equals(0)) {
					return ajaxError("该帐单已是A帐，无需分账");
				}
				merchant_order.setAccountType(0);
			}
			merchant_order.setAccountType(0);
		}
		if (atype == 1) {
			if (!CommonUtil.isBlank(merchant_order.getAccountType())) {
				if (merchant_order.getAccountType().equals(1)) {
					return ajaxError("该帐单已是B帐，无需分账");
				}
				merchant_order.setAccountType(1);
			}
			merchant_order.setAccountType(1);
		}
		if (iMerchantOrderService.updateById(merchant_order)) {
			return ajaxSuccess();
		} else {
			return ajaxError();
		}
	}

	/**
	 * 订单数据流水
	 * 
	 * @param merchant_order
	 * @param orderProcess
	 * @param orderNum
	 * @param accountType
	 * @param shopName
	 * @param dwzPageBean
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 * @throws ParseException
	 */
	@RequestMapping("/orderFlow/orderFlow")
	@RequiresPermissions("/merchantOrder/merchantOrder/orderFlow/orderFlow")
	public Object orderFlow(@Valid MerchantOrder merchant_order, String orderProcess, String orderNum, String accountType, String shopName, DwzPageBean dwzPageBean, ModelMap map, HttpServletRequest request, HttpServletResponse response) throws ParseException {
		List<Role> rolelist = getRoleList();
		map.put("rolelist", rolelist);
		String ordernum = request.getParameter("org2.orderNum");
		String shopname = request.getParameter("org2.shopName");
		Wrapper wrapper = Condition.create().ne("mo.del", 1).like("order_process", orderProcess).like("order_num", ordernum).like("account_type", accountType).like("s.shop_name", shopname);
		if (!CommonUtil.isBlank(orderProcess)) {
			dwzPageBean.getCountResultMap().put("orderProcess", orderProcess);
		}
		if (!CommonUtil.isBlank(accountType)) {
			dwzPageBean.getCountResultMap().put("accountType", accountType);
		}
		dwzPageBean.getCountResultMap().put("orderNum", ordernum);
		dwzPageBean.getCountResultMap().put("shopName", shopname);
		// 字段排序
		if (!CommonUtil.isBlank(dwzPageBean.getOrderField()) && !CommonUtil.isBlank(dwzPageBean.getOrderDirection())) {
			dwzPageBean.getCountResultMap().put("orderField", dwzPageBean.getOrderField());
			dwzPageBean.getCountResultMap().put("orderDirection", dwzPageBean.getOrderDirection());
			wrapper.orderBy(dwzPageBean.getOrderField(), CommonUtil.isEquals(dwzPageBean.getOrderDirection(), "asc"));
		} else {
			wrapper.orderBy("mo.add_time", false);
		}
		Page resultPage = iMerchantOrderService.selectOrderFlowPage(dwzPageBean.toPage(), wrapper);
		map.put("dwzPageBean", dwzPageBean.toDwzPageBean(resultPage));
		return "merchantOrder/merchantOrder/orderFlow/flowlist";
	}

	/**
	 * 查询订单编号
	 * 
	 * @param orderNum
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getOrderNum")
	@ResponseBody
	public Object getOrderNum(String orderNum, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("order_num", orderNum);
		List<MerchantOrder> list = iMerchantOrderService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 查询店铺名称
	 * 
	 * @param shopName
	 * @param request
	 * @param response
	 * @param map
	 * @return
	 */
	@RequestMapping("/getShopName")
	@ResponseBody
	public Object getShopName(String shopName, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Wrapper wrapper = Condition.create().like("shop_name", shopName).ne("del", 1).eq("shop_state", 1);
		List list = iShopService.selectList(wrapper);
		return JSON.toJSONString(list);
	}

	/**
	 * 导出Excel
	 * 
	 * @param map
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/export")
	@RequiresPermissions("/merchantOrder/merchantOrder/export")
	public String export(ModelMap map, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 从数据库中查找数据
		Wrapper wrapper = Condition.create().ne("mo.del", 1);
		// 根据用户id获取角色
		boolean isDirector = true;
		List<Role> roles = getRoleList();
		for (Role role : roles) {
			if ("运营主管".equals(role.getName())) {
				isDirector = false;
			}
		}
		if (isDirector) {
			wrapper.eq("maid", getUserId());
		}
		Page page = dwzPageBean.toPage();
		page.setSize(Page.NO_ROW_LIMIT);
		page.setCurrent(1);
		List list = iMerchantOrderService.selectOrderPage(page, wrapper).getRecords();
		ExportParams params = new ExportParams(null, "订单记录", ExcelType.XSSF);
		map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
		map.put(NormalExcelConstants.CLASS, MerchantOrderVo.class);// 导出实体
		map.put(NormalExcelConstants.PARAMS, params);// 参数
		map.put(NormalExcelConstants.FILE_NAME, "订单记录");// 文件名称
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 财务中心导出EXCEL
	 * 
	 * @param map
	 * @param str
	 * @param dwzPageBean
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/financialexport")
	@RequiresPermissions("/merchantOrder/merchantOrder/financialexport")
	public String financialexport(ModelMap map, String shopId, String str, DwzPageBean dwzPageBean, HttpServletRequest request, HttpServletResponse response) {
		// 判断导出列表
		if (str.equals("atype")) {
			// 订单分账列表导出
			// 从数据库中查找数据
			Wrapper wrapper = Condition.create().ne("mo.del", 1).eq("mo.order_process", 9);
			// 根据用户id获取角色
			boolean isDirector = true;
			List<Role> roles = getRoleList();
			for (Role role : roles) {
				if ("运营主管".equals(role.getName())) {
					isDirector = false;
				}
			}
			if (isDirector) {
				wrapper.eq("maid", getUserId());
			}
			Page page = dwzPageBean.toPage();
			page.setSize(Page.NO_ROW_LIMIT);
			page.setCurrent(1);
			List list = iMerchantOrderService.selectVoAccountType(page, wrapper).getRecords();
			ExportParams params = new ExportParams(null, "订单分账", ExcelType.XSSF);
			IExcelDataHandler dataHandler = new MoneyExcelHandler();
			dataHandler.setNeedHandlerFields(new String[] { "订单金额", "应收金额", "实收金额" });
			params.setDataHandler(dataHandler);
			map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
			map.put(NormalExcelConstants.CLASS, AccountTypeExcelVo.class);// 导出实体
			map.put(NormalExcelConstants.PARAMS, params);// 参数
			map.put(NormalExcelConstants.FILE_NAME, "订单分账");// 文件名称
		}
		// 判断导出列表
		if (str.equals("oflow")) {
			// 订单流水列表导出
			// 从数据库中查找数据
			Wrapper wrapper = Condition.create().ne("mo.del", 1);
			// 根据用户id获取角色
			boolean isDirector = true;
			List<Role> roles = getRoleList();
			for (Role role : roles) {
				if ("运营主管".equals(role.getName())) {
					isDirector = false;
				}
			}
			if (isDirector) {
				wrapper.eq("maid", getUserId());
			}
			Page page = dwzPageBean.toPage();
			page.setSize(Page.NO_ROW_LIMIT);
			page.setCurrent(1);
			List list = iMerchantOrderService.selectOrderFlowPage(page, wrapper).getRecords();
			ExportParams params = new ExportParams(null, "订单流水", ExcelType.XSSF);
			IExcelDataHandler dataHandler = new MoneyExcelHandler();
			dataHandler.setNeedHandlerFields(new String[] { "订单金额", "应收金额", "实收金额" });
			params.setDataHandler(dataHandler);
			map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
			map.put(NormalExcelConstants.CLASS, OrderFlowExcelVo.class);// 导出实体
			map.put(NormalExcelConstants.PARAMS, params);// 参数
			map.put(NormalExcelConstants.FILE_NAME, "订单流水");// 文件名称
		}
		// 判断导出列表
		if (str.equals("shoplist")) {
			// 订单流水列表导出
			// 从数据库中查找数据
			Wrapper wrapper = Condition.create().ne("shop_state", 0).ne("s.del", 1);
			// 根据用户id获取角色
			boolean isDirector = true;
			List<Role> roles = getRoleList();
			for (Role role : roles) {
				if ("运营主管".equals(role.getName())) {
					isDirector = false;
				}
			}
			if (isDirector) {
				wrapper.eq("maid", getUserId());
			}
			Page page = dwzPageBean.toPage();
			page.setSize(Page.NO_ROW_LIMIT);
			page.setCurrent(1);
			List list = iMerchantOrderService.selectVoByshopid(page, wrapper).getRecords();
			ExportParams params = new ExportParams(null, "门店列表", ExcelType.XSSF);
			IExcelDataHandler dataHandler = new MoneyExcelHandler();
			dataHandler.setNeedHandlerFields(new String[] { "订单总额" });
			params.setDataHandler(dataHandler);
			map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
			map.put(NormalExcelConstants.CLASS, ShopListExcelVo.class);// 导出实体
			map.put(NormalExcelConstants.PARAMS, params);// 参数
			map.put(NormalExcelConstants.FILE_NAME, "门店列表");// 文件名称
		}
		// 判断导出列表
		if (str.equals("listOrder")) {
			// 订单流水列表导出
			// 从数据库中查找数据
			Wrapper wrapper = Condition.create().eq("mo.shop_id", shopId).eq("order_process", 9);
			// 根据用户id获取角色
			boolean isDirector = true;
			List<Role> roles = getRoleList();
			for (Role role : roles) {
				if ("运营主管".equals(role.getName())) {
					isDirector = false;
				}
			}
			if (isDirector) {
				wrapper.eq("maid", getUserId());
			}
			Page page = dwzPageBean.toPage();
			page.setSize(Page.NO_ROW_LIMIT);
			page.setCurrent(1);
			List list = iMerchantOrderService.selectShopOL(page, wrapper).getRecords();
			ExportParams params = new ExportParams(null, "订单详情", ExcelType.XSSF);
			IExcelDataHandler dataHandler = new MoneyExcelHandler();
			dataHandler.setNeedHandlerFields(new String[] { "订单总额", "应收金额", "实收金额" });
			params.setDataHandler(dataHandler);
			map.put(NormalExcelConstants.DATA_LIST, list); // 数据集合
			map.put(NormalExcelConstants.CLASS, ListOrderExcelVo.class);// 导出实体
			map.put(NormalExcelConstants.PARAMS, params);// 参数
			map.put(NormalExcelConstants.FILE_NAME, "订单详情");// 文件名称
		}
		return NormalExcelConstants.EASYPOI_EXCEL_VIEW;// View名称
	}

	/**
	 * 添加订单
	 * 
	 * @param map
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/toInsertMerchantOrder")
	@RequiresPermissions("/merchantOrder/merchantOrder/toInsertMerchantOrder")
	public String toInsertMerchantOrder(ModelMap map, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().ne("del", 1);
		List<Shop> shops = iShopService.selectList(wrapper);
		map.put("shops", shops);

		Wrapper mpnwrapper = Condition.create().ne("del", 1);
		map.put("productNorms", iMerchantProductNormService.selectList(mpnwrapper));

		return "merchantOrder/merchantOrder/edit2";
	}

}